﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Microsoft.Phone.Controls;
using C1.Phone;

namespace ControlExplorer.View
{
    public class PanelGridSplitter 
        : Control
    {
        Grid _parentGrid;
        int _splitterColumn;
        double _widthLeft, _widthRight;

        public PanelGridSplitter()
        {
            DefaultStyleKey = typeof(PanelGridSplitter);

            Loaded += (s, a) =>
            {
                var page = this.FindParent<PhoneApplicationPage>();
                if (page != null)
                {
                    // reset layout on orientation change
                    page.OrientationChanged += (s2, a2) =>
                    {
                        _parentGrid = this.Parent as Grid;
                        var c = (int)GetValue(Grid.ColumnProperty);
                        _parentGrid.ColumnDefinitions[c - 1].Width = new GridLength(1, GridUnitType.Star);
                        _parentGrid.ColumnDefinitions[c + 1].Width = new GridLength(0, GridUnitType.Star);
                    };
                }
            };

            var listener = new C1DragHelper(this);
            listener.DragStarted += OnDragStarted;
            listener.DragDelta += OnDragDelta;
            listener.DragCompleted += OnDragCompleted;
        }

        void OnDragStarted(object sender, C1DragStartedEventArgs e)
        {
            _splitterColumn = -1;
            _parentGrid = this.Parent as Grid;
            if (_parentGrid != null && _parentGrid.ColumnDefinitions.Count > 2)
            {
                var c = (int)GetValue(Grid.ColumnProperty);
                if (c > 0 && c < _parentGrid.ColumnDefinitions.Count - 1)
                {
                    _widthLeft = _parentGrid.ColumnDefinitions[c - 1].ActualWidth;
                    _widthRight = _parentGrid.ColumnDefinitions[c + 1].ActualWidth;
                    _splitterColumn = c;
                }
            }
        }

        void OnDragDelta(object sender, C1DragDeltaEventArgs e)
        {
            if (_parentGrid != null && _splitterColumn > 0)
            {
                ApplyDelta(e.CumulativeTranslation.X);
            }
        }

        void OnDragCompleted(object sender, C1DragCompletedEventArgs e)
        {
            _splitterColumn = -1;
            _parentGrid = null;
        }

        void ApplyDelta(double dx)
        {
            var cdLeft = _parentGrid.ColumnDefinitions[_splitterColumn - 1];
            var cdRight = _parentGrid.ColumnDefinitions[_splitterColumn + 1];

            if ((_widthRight - dx > 0) && (_widthLeft + dx > 0))
            {
                if (cdRight.Width.IsStar)
                {
                    cdLeft.Width = new GridLength(_widthLeft + dx, GridUnitType.Star);
                    cdRight.Width = new GridLength(_widthRight - dx, GridUnitType.Star);
                }
                else
                {
                    cdLeft.Width = new GridLength(_widthLeft + dx);
                    cdRight.Width = new GridLength(_widthRight - dx);
                }
            }
        }
    }
}
